#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int judge(vector<char> preorder, vector<char> inorder){
    if (preorder.size()==1)return 1;
    if (preorder.size()==0)return 0;
    char root=preorder[0];
    int length;
    int maxdepth=0;
    for (int i=0;i<inorder.size();i++){
        if (inorder[i]==root){
            length=i;
            break;
        }
    }
    
    vector<char> left_preorder;
    for (int i=1;i<length+1;i++)left_preorder.push_back(preorder[i]);

    vector<char> right_preorder;
    for (int i=length+1;i<preorder.size();i++)right_preorder.push_back(preorder[i]);

    vector<char> left_inorder(inorder.begin(), inorder.begin()+length);
    for (int i=0;i<length;i++)left_inorder.push_back(inorder[i]);

    vector<char> right_inorder;
    for (int i=length+1;i<preorder.size();i++)right_inorder.push_back(preorder[i]);
    
    if(left_preorder.size()!=0){
        
        int left_depth=judge(left_preorder, left_inorder);
        if (left_depth>maxdepth)maxdepth=left_depth;
    }

    if(right_preorder.size()!=0){
       
        int right_depth=judge(right_preorder, right_inorder);
        if (right_depth>maxdepth)maxdepth=right_depth;
    }
    return maxdepth+1;


}
int main(){
    vector<char> preorder;
    vector<char> inorder;

    char c;
    c=getchar();
    while (c!='\n'){
        if (c!=' ')preorder.push_back(c);
        c=getchar();
    }
 
    c=getchar();
    while (c!='\n'){
        if (c!=' ')inorder.push_back(c);
        c=getchar();
    }
    
    
    int maxdepth=judge(preorder,inorder);
    cout<<maxdepth<<endl;

    return 0;
}

